//package com.test;
//
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.fang.b.apisite.schedule.handler.BaseHandler;
//import com.fang.b.apisite.schedule.util.EmailService;
//import com.fang.b.utils.io.apicall.IApiCallerCreator;
//import com.fang.b.utils.io.apicall.ResponseDataType;
//import com.xxl.job.core.biz.model.ReturnT;
//import com.xxl.job.core.handler.annotation.JobHandler;
//import com.xxl.job.core.log.XxlJobLogger;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.poi.hssf.usermodel.HSSFCell;
//import org.apache.poi.hssf.usermodel.HSSFRow;
//import org.apache.poi.hssf.usermodel.HSSFSheet;
//import org.apache.poi.hssf.usermodel.HSSFWorkbook;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import javax.activation.DataSource;
//import javax.mail.util.ByteArrayDataSource;
//import java.io.*;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.function.Predicate;
//import java.util.stream.Collectors;
//
///**
// * poi hyj
// */
//@JobHandler("StatisticsHandler")
//@Component
//@Slf4j
//public class StatisticsHandler extends BaseHandler {
//	@Autowired
//	private IApiCallerCreator apiCallerCreator;
//	@Value("${fang.b.portalsite.aos.url}")
//	String fangbPortalsiteAosUrl;
//	@Autowired
//	EmailService emailService;
//
//	/**
//	 * {
//	 * "houseType": 1,
//	 * "statType": 2,
//	 * "to": ["huangyujun.bj@fang.com", "houyanjun@fang.com"]
//	 * }
//	 *
//	 * @param param
//	 * @return
//	 */
//	@Override
//	public ReturnT<String> go(String param) {
//		//0.parse param
//		JSONObject jsonObject = JSONObject.parseObject(param);
//		Integer houseType = jsonObject.getInteger("houseType");
//		Integer statType = jsonObject.getInteger("statType");
//		JSONArray jsonArray = jsonObject.getJSONArray("to");
//		String[] temp = new String[jsonArray.size()];
//		String[] to = jsonArray.toArray(temp);
//		if (houseType == null || statType == null || to == null) {
//			return ReturnT.FAIL;
//		}
//
//		//1.收集数据到表中
//		if (houseType.equals(0) && statType.equals(0)) {
//			String urlPath = String.format("%s/statistic?houseType=%s&statType=%s", fangbPortalsiteAosUrl, 1, 1);
//			apiCallerCreator.getCaller().setRequestUrl(urlPath).setResponseDataType(ResponseDataType.Json).get(JSONObject.class);
//			urlPath = String.format("%s/statistic?houseType=%s&statType=%s", fangbPortalsiteAosUrl, 1, 2);
//			apiCallerCreator.getCaller().setRequestUrl(urlPath).setResponseDataType(ResponseDataType.Json).get(JSONObject.class);
//			urlPath = String.format("%s/statistic?houseType=%s&statType=%s", fangbPortalsiteAosUrl, 2, 1);
//			apiCallerCreator.getCaller().setRequestUrl(urlPath).setResponseDataType(ResponseDataType.Json).get(JSONObject.class);
//			urlPath = String.format("%s/statistic?houseType=%s&statType=%s", fangbPortalsiteAosUrl, 2, 2);
//			apiCallerCreator.getCaller().setRequestUrl(urlPath).setResponseDataType(ResponseDataType.Json).get(JSONObject.class);
//		} else {
//			String urlPath = String.format("%s/statistic?houseType=%s&statType=%s", fangbPortalsiteAosUrl, houseType, statType);
//			apiCallerCreator.getCaller().setRequestUrl(urlPath).setResponseDataType(ResponseDataType.Json).get(JSONObject.class);
//		}
//
//		//2.从表中取出最终数据
//		String getAllDataUrlPath = String.format("%s/getalldata", fangbPortalsiteAosUrl);
//		List<JSONObject> allJson = apiCallerCreator.getCaller().setRequestUrl(getAllDataUrlPath).get(List.class);//"一步到位StatisticModel"做不到
//		List<StatisticModel> allData = allJson.stream().map(t -> t.toJavaObject(StatisticModel.class)).collect(Collectors.toList());
//		//3.最终数据变为Xx.xlsx,发邮件
//		dataTransToExcelThenSendMail(allData, to);
//
//		return ReturnT.SUCCESS;
//	}
//
//	private void dataTransToExcelThenSendMail(List<StatisticModel> allData, String[] to) {
////		String[] to = new String[]{"huangyujun.bj@fang.com"};
//
//		String subject = String.format("%s企业对接房源数据", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
//		String content = "生成附件如下:\r\n\t";
//		String filename = String.format("%s.xlsx", subject);//二进制文件是对的,但不加后辍
//
//		//allData 转 excel
//		List<StatisticModel> house1stat1List = allData.stream().
//				filter(statisticModel -> statisticModel.getHouseType().equals(1) && statisticModel.getStatType().equals(1)).collect(Collectors.toList());
//		List<StatisticModel> house1stat2List = allData.stream().
//				filter(statisticModel -> statisticModel.getHouseType().equals(1) && statisticModel.getStatType().equals(2)).collect(Collectors.toList());
//		List<StatisticModel> house2stat1List = allData.stream().
//				filter(statisticModel -> statisticModel.getHouseType().equals(2) && statisticModel.getStatType().equals(1)).collect(Collectors.toList());
//		List<StatisticModel> house2stat2List = allData.stream().
//				filter(statisticModel -> statisticModel.getHouseType().equals(2) && statisticModel.getStatType().equals(2)).collect(Collectors.toList());
//
//		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//		HSSFSheet house1stat1 = hssfWorkbook.createSheet("出售存量");
//		HSSFSheet house1stat2 = hssfWorkbook.createSheet("出售增量");
//		HSSFSheet house2stat1 = hssfWorkbook.createSheet("出租存量");
//		HSSFSheet house2stat2 = hssfWorkbook.createSheet("出租增量");
//
//		for (int i = 0; i < house1stat1List.size(); i++) {
////			HSSFRow row = house1stat1.getRow(i);//空的excel,这里会报错,row为null
//			HSSFRow row = house1stat1.createRow(i);
////			HSSFCell cell = row.getCell(0);//就算创建了row对象,直接getCell也是报错,cell为null
//			HSSFCell cell = row.createCell(0);
//			HSSFCell cell1 = row.createCell(1);
//			cell.setCellValue(house1stat1List.get(i).getCity());
//			cell1.setCellValue(house1stat1List.get(i).getAmount());
//		}
//		for (int i = 0; i < house1stat2List.size(); i++) {
//			HSSFRow row = house1stat2.createRow(i);
//			HSSFCell cell = row.createCell(0);
//			HSSFCell cell1 = row.createCell(1);
//			cell.setCellValue(house1stat2List.get(i).getCity());
//			cell1.setCellValue(house1stat2List.get(i).getAmount());
//		}
//		for (int i = 0; i < house2stat1List.size(); i++) {
//			HSSFRow row = house2stat1.createRow(i);
//			HSSFCell cell = row.createCell(0);
//			HSSFCell cell1 = row.createCell(1);
//			cell.setCellValue(house2stat1List.get(i).getCity());
//			cell1.setCellValue(house2stat1List.get(i).getAmount());
//		}
//		for (int i = 0; i < house2stat2List.size(); i++) {
//			HSSFRow row = house2stat2.createRow(i);
//			HSSFCell cell = row.createCell(0);
//			HSSFCell cell1 = row.createCell(1);
//			cell.setCellValue(house2stat2List.get(i).getCity());
//			cell1.setCellValue(house2stat2List.get(i).getAmount());
//		}
//
//		DataSource dataSource = null;
//		ByteArrayOutputStream byteArrayOutputStream = null;
//		ByteArrayInputStream byteArrayInputStream = null;
//		try {
//			byteArrayOutputStream = new ByteArrayOutputStream();
//
//			hssfWorkbook.write(byteArrayOutputStream);
//			hssfWorkbook.close();
//			byteArrayOutputStream.flush();
//			byte[] bytes = byteArrayOutputStream.toByteArray();
//			byteArrayInputStream = new ByteArrayInputStream(bytes);
//			byteArrayInputStream.close();
//			byteArrayOutputStream.close();
//
//			dataSource = null;
//			dataSource = new ByteArrayDataSource(byteArrayInputStream, "application/msexcel");
//		} catch (Exception e) {
//			e.printStackTrace();
//			XxlJobLogger.log(e.toString());
//		}
//		emailService.sendAttachmentsMail(to, subject, content, filename, dataSource);
//	}
//}
//
